package com.example.demo.controller;

import com.example.demo.util.officeutil.WordDemo;
import com.example.demo.util.officeutil.WordTemplate;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.InputStream;
import java.util.Map;


@RestController
@RequestMapping("/word")
public class WordController {


    @GetMapping("/download")
    public void download(HttpServletResponse response) {
        try {
            //获取模拟数据
            Map<String, Object> mockData = WordDemo.getMockData();

            //从文件系统读取文档
//            File file = new File("C:\\Users\\Administrator\\Desktop\\test\\template01.docx");   //可改成你本地模板文件所在目录
            InputStream fileInputStream = WordController.class.getClassLoader().getResourceAsStream("file/template01.docx");
//            File file = ResourceUtils.getFile("classpath:file/template01.docx");
//            FileInputStream fileInputStream = new FileInputStream(file);
            WordTemplate template = new WordTemplate(fileInputStream);

            //渲染模版数据并获取文档
            template.replaceDocument(mockData);

            //下载文档
            template.downloadDocument(response, "文档名称");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
